<?php

namespace app\admin\controller;

use app\admin\validate\SysNotice as SysNoticeValidate;
use app\common\business\SysNotice as SysNoticeBus;
use app\common\lib\toCamelCase;

class SysNotice extends AuthBase
{
    //通知公告
    public function noticeList(){
        try {
            $where = [];
            // 获取分页参数
            $page = $this->request->get('pageNum', 1);
            $limit = $this->request->get('pageSize', 10);
            $noticeTitle = $this->request->get('noticeTitle',"");
            $createBy = $this->request->get('createBy',"");
            $noticeType = $this->request->get("noticeType","");
            if($noticeTitle){
                $where[] = ['notice_title', 'like', '%'.$noticeTitle.'%'];
            }
            if($createBy){
                $where[] = ['create_by', 'like', '%'.$createBy.'%'];
            }
            if ($noticeType) {
                // 如果$status不为null且不为空字符串，则添加条件到$where数组
                $where[] = ["notice_type", "=", $noticeType];
            }
            $noticeData = (new SysNoticeBus())->getNormalNotice($where,'*',$limit,$page);
            $noticeData = (new toCamelCase())->toCamelCaseArray($noticeData);
        }catch (\Exception $e){
            $noticeData = [];
        }
        return show(config("status.success"),"通知公告列表",$noticeData);
    }

    //通知公告添加
    public function noticeAdd(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验
        $allParams = request()->param();
        $data = [
            'notice_content'=>$allParams['noticeContent'],
            'notice_title'=>$allParams['noticeTitle'],
            'notice_type'=>$allParams['noticeType'],
            'status'=>$allParams['status'],
        ];
        $validate = new SysNoticeValidate();
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $result = (new SysNoticeBus())->addNotice($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"新增成功",$result);
    }

    //通知公告修改
    public function noticeEdit(){
        $allParams = request()->param();
        $data = (new toCamelCase)->toCamelCaseArray($allParams,1,2);
        try {
            $result = (new SysNoticeBus())->updateById($data['notice_id'],$data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"修改成功",$result);
    }

    //通知公告详情
    public function noticeDetails(){
        $notice = $this->request->param("notice","","trim");
        if(empty($notice)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $postData = (new SysNoticeBus())->getNoticeById($notice);
            $postData = (new toCamelCase())->toCamelCaseArray($postData);
        }catch (\Exception $e){
            $postData = [];
        }
        return show(config("status.success"),"通知公告详情",$postData);
    }

    //删除通知公告
    public function noticeDel(){
        $notice = $this->request->param("notice","","trim");
        if(empty($notice)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $result = (new SysNoticeBus())->DelNoticeIds($notice);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"删除成功",$result);
    }
}
